/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Modele.DAO;
import Modele.Dep;
import Modele.Med;
import Modele.Pays;
import Modele.Spe;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sgarreau
 */
public class Control extends HttpServlet {

    private Pays pays;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        pays = new Pays();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = null;
        String choix = request.getParameter("choice1");
        String choix2 = request.getParameter("choice2");
        String choix3 = request.getParameter("choice3");
        String name = request.getParameter("name");
        String liste = request.getParameter("liste");
        String spe = request.getParameter("spe");
        if (choix != null) {
            Collection<Dep> dep = pays.getLesDeps();
            request.setAttribute("listeDep", dep);
            page = "choix1.jsp";
        }
        if (choix2 != null || name != null) {
            Collection<Dep> dep = pays.getLesDeps();
            Collection<Med> med = new TreeSet<Med>();
            for (Dep unDep : dep) {
                for (Med unMed : unDep.getLesMeds()) {
                    if (name != null) {
                        if (unMed.getNom().startsWith(name)) {
                            med.add(unMed);
                        }
                    }
                }
            }
            request.setAttribute("listeMed", med);
            page = "choix2.jsp";
        }
        if (choix3 != null) {
            Collection<Spe> spec = pays.getLesSpes();
            request.setAttribute("listeSpe", spec);
            page="choix3.jsp";
        }
        
        if (liste != null) {
            String dep = request.getParameter("dep");
            Collection<Med> med = pays.getLeDep(dep).getLesMeds();
            request.setAttribute("dep", dep);
            request.setAttribute("listeMed", med);
            page = "Liste1.jsp";
        }
        
        if (spe!=null) {
            String specia = request.getParameter("specia");
            Collection<Med> med = pays.getLaSpe(specia).getLesMeds();
            request.setAttribute("spe", specia);
            request.setAttribute("listeMed", med);
            page = "Liste2.jsp";
        }

        if (choix == null && choix2 == null && choix3 == null && name == null && liste == null && spe== null) {
            page = "index.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
